package com.vehicle.biz.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.biz.SysPermissionBiz;
import com.vehicle.domains.entity.SysPermissionEntity;
import com.vehicle.domains.vo.BasePage;
import com.vehicle.domains.bo.QueryPermissionBo;
import com.vehicle.domains.vo.req.sysPermission.AddPermissionReq;
import com.vehicle.domains.vo.req.sysPermission.ListPermissionReq;
import com.vehicle.domains.vo.req.sysPermission.UpdatePermissionReq;
import com.vehicle.domains.vo.res.sysPermission.SysPermissionRes;
import com.vehicle.exception.VehicleAccessException;
import com.vehicle.service.SysPermissionService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysPermissionBizImpl implements SysPermissionBiz {

    @Resource
    private SysPermissionService sysPermissionService;

    @Override
    public void addPermission(AddPermissionReq req) {
        SysPermissionEntity sysPermissionEntity = new SysPermissionEntity();
        BeanUtils.copyProperties(req, sysPermissionEntity);
        sysPermissionService.addPermission(sysPermissionEntity);
    }

    @Override
    public void updatePermission(UpdatePermissionReq req) {
        checkPermissionExist(req.getId());
        SysPermissionEntity sysPermissionEntity = new SysPermissionEntity();
        BeanUtils.copyProperties(req, sysPermissionEntity);
        sysPermissionService.updatePermission(sysPermissionEntity);
    }

    @Override
    public void deletePermission(Long id) {
        checkPermissionExist(id);
        sysPermissionService.deletePermission(id);
    }

    @Override
    public SysPermissionRes getPermissionById(Long id) {
        SysPermissionEntity sysPermissionEntity = checkPermissionExist(id);
        SysPermissionRes sysPermissionRes = new SysPermissionRes();
        BeanUtils.copyProperties(sysPermissionEntity, sysPermissionRes);
        return sysPermissionRes;
    }

    @Override
    public BasePage<SysPermissionRes> selectPermissionList(ListPermissionReq req) {
        Page<SysPermissionEntity> page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        QueryPermissionBo permissionBo = new QueryPermissionBo();
        BeanUtils.copyProperties(req, permissionBo);
        List<SysPermissionEntity> sysPermissionEntityList= sysPermissionService.selectPermissionList(permissionBo);
        List<SysPermissionRes> list = sysPermissionEntityList.stream().map(sysPermissionEntity -> {
            SysPermissionRes sysPermissionRes = new SysPermissionRes();
            BeanUtils.copyProperties(sysPermissionEntity, sysPermissionRes);
            return sysPermissionRes;
        }).toList();
        return BasePage.generateResult(page.getTotal(), page.getPages(), list);
    }

    private SysPermissionEntity checkPermissionExist(Long id) {
        SysPermissionEntity sysPermissionEntity=sysPermissionService.selectByPermissionId(id);
        if (sysPermissionEntity==null){
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("权限不存在");
        }
        return sysPermissionEntity;
    }
}
